#include "Widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QFileDialog>
#include <QDebug>
#include <QKeyEvent>
#include <QTimer>
#include <QMouseEvent>
#include <cmath>
#include <QEvent>

extern GameMap* gameMap;
extern Tank* player[4];
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("Tank 2022");
    this->setCursor(QCursor(QPixmap(":/map_image/element/Cursor.png")));
    this->setWindowIcon(QIcon(":/map_image/element/TankICO.ico"));
    mypainter = new QPainter;
    gameMap = new GameMap(this);
    player[0] = new Tank(Qt::Key_W,Qt::Key_S,Qt::Key_A,Qt::Key_D,Qt::Key_J,this);
    player[1] = new Tank(Qt::Key_Up,Qt::Key_Down,Qt::Key_Left,Qt::Key_Right,Qt::Key_Enter,this);
    player[2] = new Tank(Qt::Key_G,Qt::Key_B,Qt::Key_V,Qt::Key_N,Qt::Key_Space,this);
    player[3] = new Tank(Qt::Key_4,Qt::Key_5,Qt::Key_4,Qt::Key_6,Qt::Key_Plus,this);
    player[0] -> isCreated=true;
    player[1] -> isCreated =true;
    timer=new QTimer(this);
    timer->start(100);
    connect(timer,&QTimer::timeout,[this](){update();});
    QString filename = QFileDialog::getOpenFileName(this,"选择地图文件","./","*.txt");
    gameMap->iniByTxt(filename);
    this->setMouseTracking(true);
    this->setFixedSize(QImage(":/map_image/element/wall.gif").width()*(gameMap->col),QImage(":/map_image/element/wall.gif").width()*(gameMap->row));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::DrawMap()
{
    for(int i=0;i<gameMap->row;i++)
    {
        for(int j=0;j<gameMap->col;j++)
        {
            QString imageUrl;
            switch(gameMap->Gmap[i][j])
            {
            case road:imageUrl=":/map_image/element/road.png";break;
            case wall:imageUrl=":/map_image/element/wall.gif";break;
            case grass:imageUrl=":/map_image/element/grass.gif";break;
            }
            QImage image(imageUrl);
            mypainter->drawImage(QRectF(j*image.width(),i*image.height(),image.width(),image.height()),image);
        }
    }
}

void Widget::paintEvent(QPaintEvent* event)
{
    Q_UNUSED(event);
    mypainter->begin(this);
    DrawMap();
    for(int i=0;i<4;i++)
    {
        if(player[i]->isCreated == true)
        player[i]->DrawTank(mypainter,player[i]->mydir);
    }
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<4;j++)
        {
            if(player[j]->isCreated == true)
            {
                player[j]->mybullet[i].DrawBullet(mypainter);
                if(player[j]->mybullet[i].isshoot == true &&player[j]->mybullet[i].iscollide()) player[j]->mybullet[i].isshoot=false;
            }
        }
    }
    mypainter->end();
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    for(int i=0;i<4;i++)
    {
        if(player[i]->isCreated == true)
        {
            player[i]->HowMove(event);
            player[i]->HowShoot(event);
        }
    }
    this->update();
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    //实现player2方向跟随鼠标光标
    QPoint TankVector=QPoint(event->pos().x()-player[1]->pos_x,event->pos().y()-player[1]->pos_y);
    if(TankVector.x()>0 && TankVector.x() >= abs(TankVector.y()))
    {
        player[1]->mydir = xright;
    }
    else if(TankVector.x()<0 && -TankVector.x() >= abs(TankVector.y()))
    {
        player[1]->mydir = xleft;
    }
    else if(TankVector.y()>0 && TankVector.y() >= abs(TankVector.x()))
    {
        player[1]->mydir = xdown;
    }
    else if(TankVector.y()<0 && -TankVector.y() >= abs(TankVector.x()))
    {
        player[1]->mydir = xup;
    }
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    //实现player2使用鼠标射击
    if(event->buttons() & Qt::LeftButton)
    {
        for(int i=0;i<5;i++)
        {
            if(player[1]->mybullet[i].isshoot == false)
            {
                player[1]->mybullet[i].my_dir=player[1]->mydir;
                player[1]->mybullet[i].pos_x=player[1]->pos_x+24;
                player[1]->mybullet[i].pos_y=player[1]->pos_y+24;
                player[1]->mybullet[i].isshoot = true;
                break;
            }
        }
    }

}
